#https://blog.csdn.net/fenglingguitar/article/details/146056008
import os
from langchain_community.llms import Tongyi
#https://blog.csdn.net/zhishi0000/article/details/146470590
from langchain.agents import initialize_agent, load_tools, AgentType
import json

llm = Tongyi()
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from langchain.tools import Tool
from wwmath import funsig
from other.yolo import myolo
from langchain_core.prompts import ChatPromptTemplate,PromptTemplate
# 定义工具：计算平方
def square_number(n: str):
    return int(n) ** 2

def sigx(arg):

     

    print(arg)
    print(type(arg))
    agrs=arg.split(",")
    xx = [int(item) for item in agrs]
    print((xx))
    z=funsig.funcall("fun1",xx)
    return z;


def sigx2(arg):

     

    print(arg)
    print(type(arg))
    arg = json.loads(arg)  
    xx = [arg["yw"],arg["sx"]]
    print((xx))
    z=funsig.funcall("fun1",xx)
    return z;

def yl(arg):
    print(arg)
    print(type(arg))
    rs=myolo.pred(arg)
    p=0
    for r in rs:
        print("")
        print(r.boxes.cls )
        print(r.boxes.conf)
        for j in range(r.boxes.cls.size(0)):
            cs=r.boxes.cls[j]
            if(cs==0):
                p=p+1
    return p;
                
square_tool = Tool(name="Square Calculator", func=square_number, description="计算一个数的平方")
sigx_tool = Tool(name="sigx", func=sigx, description="根据语文、数学两个科目的绩点计算升学权重,输入参数是参数用英文逗号分开，例如：1,2")
sigx_tool2 = Tool(name="sigx", func=sigx2, description="根据语文(yw)、数学(sx)两个科目的绩点计算升学权重,输入参数是json 格式例如，")
yl_tool2 = Tool(name="yl", func=yl, description="根据图片信息检测，统计图片中的人数，输入参数为图片路径，例如：aa/dd/cc.jpg;")


# 初始化代理
agent = initialize_agent(
    tools=[square_tool,sigx_tool2,yl_tool2],
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)
 

def test():
    #response = agent.run("我的语文时 9 个绩点，英语是 10 个绩点，数学是8个绩点，我要计算升学权重")
    #agent2=prompt|agent
    response = agent.run( "根据图片检测的人数，推荐一个合适的交通工具,图片路径是:D:/gitee/yolo11/bus.jpg;" )
   
    print(response)  # 输出: 25
    return response

 
